package com.apes.scm.settle.payment.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.income.model.IncomeItem;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.finance.model.IncomeType;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * 付款费用登记
 *
 * @author muting
 * @create 2022-07-21 14:24
 */

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PaymentCost.CONST_TABLE_NAME)
public class PaymentCost extends Model {
    public static final String CONST_TABLE_NAME = "acc_payment_cost";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "费用付款明细编码")
    private String id;

    @NotNull
    @Column(name = "income_Id", length = 32)
    @Comment("收入单号")
    private String incomeId;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @Comment("收入明细编码")
    @JoinColumn(name = "income_Item_Id", referencedColumnName = "id")
    private IncomeItem incomeItemId;

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "paymen_Registert_id", referencedColumnName = "id")
    @Comment("付款单编码")
    private PaymentRegister paymentCost; /*付款登记主表*/

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "income_category", referencedColumnName = "id")
    @Comment("收入类别")
    private Enumeration inComeCategory;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "income_type_id", referencedColumnName = "id")
    @Comment("收入类型")
    private IncomeType incomeType;  //收入类型

    @Column(name = "income_Amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("收入金额")
    private double incomeAmount = 0; //收入金额

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("本次扣款金额")
    private double amount = 0; // 本次扣款金额

    @Transient
    private double recoveryAmount = 0; // 已扣款金额

    public double getRecoveryAmount() {
        return this.incomeItemId.getRecoveryAmount();
    }

    @Transient
    private double freezeAmount = 0;//冻结金额

    public double getFreezeAmount() {
        return this.incomeItemId.getFreezeAmount();
    }
}
